import { defineStore } from 'pinia'
import { Comment } from "../../types/JSJL/CommentStore";

// 使用教师交流评论区使用
export const useCommentStore = defineStore('comments', {
    state: () => ({
        comments: [
            {
                id: '1',
                author: '呼哈',
                content: '这是一条测试内容',
                time: '21:26',
                replies: [
                    {
                        id: '1-1',
                        author: '呼哈 test',
                        content: '偶尔做会小孩\n回复呼哈：测试',
                        time: '21:26'
                    }
                ]
            },
            {
                id: '2',
                author: '偶尔做会小孩',
                content: '晚上了',
                time: '21:26',
                replies: []
            }
        ] as Comment[],
        newComment: ''
    }),
    actions: {
        addComment() {
            if (!this.newComment.trim()) return

            this.comments.push({
                id: Date.now().toString(),
                author: '当前用户',
                content: this.newComment,
                time: new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }),
                replies: []
            })
            this.newComment = ''
        },
        addReply(commentId: string, content: string) {
            const comment = this.comments.find(c => c.id === commentId)
            if (comment) {
                comment.replies?.push({
                    id: `${commentId}-${Date.now()}`,
                    author: '当前用户',
                    content,
                    time: new Date().toLocaleTimeString([], {hour: '2-digit', minute: '2-digit'})
                })
            }
        }  }
    })
